package com.instagram.instadownloader.utils.download;

import android.app.Activity;

import com.instagram.instadownloader.utils.InstaFileManager;
import com.instagram.instadownloader.utils.SdcardUtils;
import com.instagram.instadownloader.utils.download.DownloadTask.OnDownloadListener;

/**
 * The Class InstaDownloadManager.
 * @author HuanND
 */
public class InstaDownloadManager {

//	/** The m download manager. */
//	private DownloadManager mDownloadManager;

	/** The m activity. */
	private Activity mActivity;

	private HandlerDownload mHandler;
	/**
	 * Instantiates a new insta download manager.
	 *
	 * @param activity the activity
	 */
	public InstaDownloadManager(Activity activity) {
		mActivity = activity;
//		configure();
	}

//	/**
//	 * Configure.
//	 */
//	private void configure() {
//		mDownloadManager = (DownloadManager) mActivity
//				.getSystemService(Activity.DOWNLOAD_SERVICE);
//	}
//
//
//	/**
//	 * Start download.
//	 *
//	 * @param url the url
//	 * @param fileDir the file dir
//	 * @param fileName the file name
//	 */
//	private long startDownload(String url, String fileDir, String fileName) {
//		Uri uri = Uri.parse(url);
//		Request request = new DownloadManager.Request(uri)
//									.setAllowedNetworkTypes(
//											DownloadManager.Request.NETWORK_WIFI | 
//											DownloadManager.Request.NETWORK_MOBILE)
//									.setAllowedOverRoaming(false).setTitle(fileName)
//									/*	.setNotificationVisibility(View.VISIBLE)*/
//									.setDescription("Something useful. No, really.")
//									.setDestinationInExternalPublicDir("instagramDownloader", fileName);
//		((MainActivity) mActivity).setCurrentFIleName(InstaFileManager.INSTA_DOWNLOADER_FOLDER + fileName);
//		if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
//			request.setNotificationVisibility(View.VISIBLE);
//		}
//		return mDownloadManager.enqueue(request);
//	}
//	
//	/**
//	 * Start download.
//	 *
//	 * @param url the url
//	 * @param fileDir the file dir
//	 * @param fileName the file name
//	 */
//	private Request startDownloadWithProgress(String url, String fileDir, String fileName) {
//		Uri uri = Uri.parse(url);
//		Request request = new DownloadManager.Request(uri)
//									.setAllowedNetworkTypes(
//											DownloadManager.Request.NETWORK_WIFI | 
//											DownloadManager.Request.NETWORK_MOBILE)
//									.setAllowedOverRoaming(false).setTitle(fileName)
//									.setDescription("Something useful. No, really.")
//									.setDestinationInExternalPublicDir("instagramDownloader", fileName);
//		if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
//				request.setNotificationVisibility(View.VISIBLE);
//		}
//		((MainActivity) mActivity).setCurrentFIleName(InstaFileManager.INSTA_DOWNLOADER_FOLDER + fileName);
//		return request;
//	}
//	
//	/**
//	 * Start download.
//	 *
//	 * @param url the url
//	 * @param fileDir the file dir
//	 * @param fileName the file name
//	 */
//	public long startDownload(String url, String absolutePath) {
//		Uri uri = Uri.parse(url);
//		Request request = new DownloadManager.Request(uri)
//									.setAllowedNetworkTypes(
//											DownloadManager.Request.NETWORK_WIFI | 
//											DownloadManager.Request.NETWORK_MOBILE)
//											/*.setNotificationVisibility(View.VISIBLE)*/
//									.setAllowedOverRoaming(false).setTitle(FilenameUtils.getName(absolutePath))
//									.setDescription("Something useful. No, really.")
//									.setDestinationUri(Uri.parse(absolutePath));
//		return mDownloadManager.enqueue(request);
//	}
//	
//	/**
//	 * Display download.
//	 */
//	public void displayDownload(){
//		Intent intent = new Intent();
//		intent.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
//		mActivity.startActivity(intent);
//	}
//	
//	/**
//	 * Cancel download.
//	 *
//	 * @param fileDownloadId the file download id
//	 */
//	public void cancelDownload(long fileDownloadId){
//		mDownloadManager.remove(fileDownloadId);
//	}
//	
//	/**
//	 * Open download file.
//	 *
//	 * @param fileDownloadId the file download id
//	 */
//	public void openDownloadFile(long fileDownloadId){
//		try {
//			mDownloadManager.openDownloadedFile(fileDownloadId);
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
//	}
	

//	double currentVl = 0;
//	/**
//	 * Check status.
//	 * 
//	 * @param fileDownloadId
//	 *            the file download id
//	 */
//	private int checkStatus(Request rs) {
//		final long downloadId = mDownloadManager.enqueue(rs);
//
//		new Thread(new Runnable() {
//			
//			@Override
//			public void run() {
//				
//				boolean downloading = true;
//
//				while (downloading) {
//					// Query the download manager about downloads that have been requested.
//					DownloadManager.Query q = new DownloadManager.Query();
//					Cursor cursor = mDownloadManager.query(q);
//					q.setFilterById(downloadId);
//
//					cursor.moveToFirst();
//					int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
//					int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
//
//					if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
//						downloading = false;
//					}
//
//					final double dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
//					
//					mActivity.runOnUiThread(new Runnable() {
//						
//						@Override
//						public void run() {
//							if (processDownloading != null) {
//								if(dl_progress > currentVl){
//									currentVl = dl_progress;
//									processDownloading.onChanging(dl_progress);
//								}
//								
//							}
//						}
//					});
//					
//					cursor.close();
//				}
//
//			}
//		}).start();
//
//		return -1;
//	}
	
	public void setHandlerDownload(HandlerDownload handler){
		mHandler = handler;
	}
	
	public boolean runDownload(String url, String fileDir, String fileName,OnDownloadListener listener){
		boolean ret = true;
		if(SdcardUtils.hasStorage()){
//			this.checkStatus(startDownloadWithProgress(url, fileDir, fileName));
			String absolutePath = InstaFileManager.INSTA_DOWNLOADER_FOLDER + fileName;
			new DownloadTask(url, absolutePath, listener,mHandler).execute();
//			startDownload(url, fileDir, fileName);
		}else{
			ret = false;
		}
		return ret;
	}
	
//	private OnProcessDownloading processDownloading;
//	public void setOnProcessDownloadingListener(OnProcessDownloading processDownloading) {
//		this.processDownloading = processDownloading;
//	}
//	
//	public interface OnProcessDownloading{
//		void onChanging(double value);
//	}
	
}
